package com.nsgf.library.ui.adapter;

import static androidx.databinding.adapters.TextViewBindingAdapter.setTextSize;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.nsgf.library.data.bean.Category;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ItemSubCategoryItemBinding;

import java.util.List;

public class SecondaryCategoryAdapter extends RecyclerView.Adapter<SecondaryCategoryAdapter.ViewHolder> {
    private Context context;
    private List<Category> categories;
    private OnSecondaryCategoryClickListener listener;
    private OnScrollListener scrollListener;
    private LinearLayoutManager layoutManager;

    public interface OnSecondaryCategoryClickListener {
        void onSecondaryCategoryClick(Category category);
    }

    public interface OnScrollListener {
        void onScroll(int position);
    }

    public SecondaryCategoryAdapter(Context context, List<Category> categories,
                                    OnSecondaryCategoryClickListener listener,
                                    OnScrollListener scrollListener) {
        this.context = context;
        this.categories = categories;
        this.listener = listener;
        this.scrollListener = scrollListener;
    }

    public String getCategoryTitle(int position) {
        if (position >= 0 && position < categories.size()) {
            return categories.get(position).getTitle();
        }
        return null;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_sub_category, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Category category = categories.get(position);

        // 设置分类标题
        holder.categoryTitle.setText(category.getTitle());

        // 设置子分类列表
        SubCategoryAdapter subAdapter = new SubCategoryAdapter(context,
                category.getList(), listener);
        holder.subCategoryList.setAdapter(subAdapter);
        holder.subCategoryList.setLayoutManager(new LinearLayoutManager(context));
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (scrollListener != null && layoutManager != null) {
                    int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                    if (firstVisibleItemPosition != RecyclerView.NO_POSITION) {
                        scrollListener.onScroll(firstVisibleItemPosition);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return categories != null ? categories.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView categoryTitle;
        RecyclerView subCategoryList;

        ViewHolder(View itemView) {
            super(itemView);
            categoryTitle = itemView.findViewById(R.id.category_title);
            subCategoryList = itemView.findViewById(R.id.sub_category_list);
        }
    }

    // 子分类列表适配器
    private static class SubCategoryAdapter extends RecyclerView.Adapter<SubCategoryAdapter.ViewHolder> {
        private Context context;
        private List<Category> subCategories;
        private OnSecondaryCategoryClickListener listener;

        SubCategoryAdapter(Context context, List<Category> subCategories,
                           OnSecondaryCategoryClickListener listener) {
            this.context = context;
            this.subCategories = subCategories;
            this.listener = listener;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_sub_category_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Category subCategory = subCategories.get(position);
            holder.binding.setVm(subCategory);
            holder.total_old_price.setPaintFlags(holder.total_old_price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

            holder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onSecondaryCategoryClick(subCategory);
                }
            });
        }

        @Override
        public int getItemCount() {
            return subCategories != null ? subCategories.size() : 0;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView subCategoryName;
            TextView total_old_price;
            ItemSubCategoryItemBinding binding;

            ViewHolder(View itemView) {
                super(itemView);
                subCategoryName = itemView.findViewById(R.id.sub_category_name);
                total_old_price = itemView.findViewById(R.id.total_old_price);
                binding = DataBindingUtil.bind(itemView);
            }
        }
    }
} 